package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntBudgetCheckMapper;
import com.prd.model.EntBudgetCheck;
import com.prd.model.LoginUser;
import com.prd.service.EntBudgetCheckService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntBudgetCheckServiceImpl extends ServiceImpl<EntBudgetCheckMapper, EntBudgetCheck> implements EntBudgetCheckService {

    @Autowired
    private EntBudgetCheckMapper entBudgetCheckMapper;

    @Override
    public int insertInfo(EntBudgetCheck entBudgetCheck) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntBudgetCheck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntBudgetCheck::getBudgetId, entBudgetCheck.getBudgetId());
        queryWrapper.eq(EntBudgetCheck::getType, entBudgetCheck.getType());
        queryWrapper.eq(EntBudgetCheck::getCreateId, loginUser.getId());
        EntBudgetCheck budgetCheck = entBudgetCheckMapper.selectOne(queryWrapper);
        if (budgetCheck != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entBudgetCheck);
        return entBudgetCheckMapper.insert(entBudgetCheck);
    }

    @Override
    public int updateInfo(EntBudgetCheck entBudgetCheck) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entBudgetCheck);
        return entBudgetCheckMapper.updateById(entBudgetCheck);
    }
}
